#include"ip.h"
#include"udp.h"
#include"tcp.h"

#define IP_LENGTH 16

IPHeader_t *ip_header;
char src_ip[IP_LENGTH];
char des_ip[IP_LENGTH];

//初始化填充ipheader
void init_ip(FILE *fp){
    ip_header = (IPHeader_t *)malloc(sizeof(IPHeader_t));
    memset(ip_header, 0, sizeof(IPHeader_t));
    if (fread(ip_header, sizeof(IPHeader_t), 1, fp) != 1)
    {
        printf(" can not read ip_header\n");
    }
}

// 根据类型获得ip
char *get_src_ip(){
    inet_ntop(AF_INET, (void *)&(ip_header->SrcIP), src_ip, IP_LENGTH);
    return src_ip;
}

char *get_des_ip(){
    inet_ntop(AF_INET, (void *)&(ip_header->DstIP), des_ip, IP_LENGTH);
    return des_ip;
}

//获取协议
int get_protocol(){
    return ip_header->Protocol;
}

void free_ip(){
    free(ip_header);
}

void process_ip(FILE *fp){
    init_ip(fp);
    printf("src_ip:%s\t\t des_ip:%s\n",get_src_ip(),get_des_ip());

    switch (get_protocol())
    {
    case PROTOCAL_TCP:
        process_tcp(fp);
        break;
    case PROTOCAL_UDP:
        process_udp(fp);
        break;
    default:
        printf("next part is not tcp or udp\n");
        break;
    }

    free_ip();
}
